package com.wanxi.controller.user;


import com.wanxi.domain.PageModel;
import com.wanxi.domain.ResultModel;
import com.wanxi.domain.user.UserModel;
import com.wanxi.domain.user.vo.UserModelVo;
import com.wanxi.domain.user.vo.UserPageVo;
import com.wanxi.service.user.IUserService;
import com.wanxi.util.Base64Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.List;


@RestController
@RequestMapping("user")
@CrossOrigin(allowCredentials = "true", origins = "http://localhost:63342", allowedHeaders = "*")//允许跨域请求
public class UserController {

    @Autowired
    private IUserService userService;


    /*查询所有用户*/
    @PostMapping("allUser")
    public List<UserModel> getAllUserInfo(@RequestBody PageModel pageModel) {

        //当前页
        int curr = (pageModel.getCurr() - 1) * pageModel.getLimit();

        List<UserModel> resultModel = userService.selectAll(curr, pageModel.getLimit());

        return resultModel;
    }

    /*查询所有用户数量*/
    @GetMapping("count")
    public int getCount(HttpServletRequest request) throws FileNotFoundException {
        String username = request.getHeader("username");
        int count = userService.getCount();
        return count;
    }

    /*根据用户id查询用户信息*/
    @GetMapping("findById")
    public UserModel findById(@RequestParam("id") Integer uid) {

        UserModel resultModel = userService.selectById(uid);

        return resultModel;
    }

    /*改变用户信息*/
    @PostMapping("update")
    public int updatePassword(@RequestBody UserModel userModel) {

        /*密码加密*/
        if (userModel.getPassword() != null) {
            userModel.setPassword(Base64Code.enCode(userModel.getPassword()));
        }

        //直接利用fastJson将json格式的字符串转换为Json对象并封装到实体类中

        int resultModel = userService.update(userModel);

        return resultModel;
    }

    /*删除某一行记录*/
    @PostMapping("delete")
    public int deleteById(@RequestBody UserModel userModel) {

        int resultModel = userService.deleteById(userModel);

        return resultModel;
    }

    /*添加用户*/
    @PostMapping("insertUser")
    public int insertUser(@RequestBody UserModel userModel) {


        /*密码加密*/
        if (userModel.getPassword() != null) {
            userModel.setPassword(Base64Code.enCode(userModel.getPassword()));
        }
        userModel.setEnable(1);
        int resultModel = userService.insertUser(userModel);

        return resultModel;

    }

    /*模糊查询数量*/
    @PostMapping("getGlobalSearchCount")
    public int getGlobalCount(@RequestBody UserModelVo userModelVo) {
        if (userModelVo.getStartDate().equals("")) {
            userModelVo.setStartDate(null);
        }
        if (userModelVo.getEndDate().equals("")) {
            userModelVo.setEndDate(null);
        }

        int globalSearchCount = userService.getGlobalSeachCount(userModelVo);

        return globalSearchCount;

    }

    /*模糊查询*/
    @PostMapping("globalSearch")
    public List<UserModel> globalSearch(@RequestBody UserPageVo userPageVo) {

        if (userPageVo.getUserModelVo().getStartDate().equals("")) {
            userPageVo.getUserModelVo().setStartDate(null);
        }
        if (userPageVo.getUserModelVo().getEndDate().equals("")) {
            userPageVo.getUserModelVo().setEndDate(null);
        }

        int curr = (userPageVo.getPageModel().getCurr() - 1) * userPageVo.getPageModel().getLimit();
        userPageVo.getPageModel().setCurr(curr);

        List<UserModel> resultModel = userService.globalSearch(userPageVo);

        return resultModel;
    }


}
